layui.use(['form', 'layer'], function () {
    const form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    // 加载
    $.ajax({
        url: $.cookie("tempUrl") + "hSysDoctorServicePack/selectByPrimaryKey?id=" + $.cookie("tempId"),
        type: "GET",
        success: function (result) {
            if (result.code === 200) {
                //给表单赋值
                form.val("formTest", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "name": result.data.name // "name": "value"
                    , "types": result.data.types
                    , "introduce": result.data.introduce
                    , "ageLimit": result.data.ageLimit
                    , "num": result.data.num
                    , "money": result.data.money
                    , "sate": result.data.sate === 1,
                });
                form.render();
            } else {
                layer.msg('查询异常', {icon: 7, anim: 6});
            }
        }
    });

    //点击取消
    $("body").on("click", "#cancelNews", function () {
        parent.layer.closeAll("iframe")
    });

    form.on("submit(addNews)", function (data) {
        var data1 = form.val("formTest");
        // layer.confirm('提交后系统会进行审核，预计审核时间0.5天，请及时关注短信', {area: ["240px", "190px"], title: '系统提示'}, function (index0) {
            //do something
            $.ajax({
                url: $.cookie("tempUrl") + "hSysDoctorServicePack/updateSelective",
                type: "PUT",
                datatype: "application/json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    id: $.cookie("tempId"),
                    ageLimit: data1.ageLimit,
                    doctorId: $.cookie("doctorId"),
                    introduce: data1.introduce,
                    money: data1.money,
                    name: data1.name,
                    num: data1.num,
                    sate: data1.sate === "on" ? "1" : "2",
                    types: data1.types,
                }),
                success: function (result) {
                    if (result.code === 200) {
                        layer.msg("更新成功");
                        setTimeout(function () {
                            // top.layer.close(index);
                            // top.layer.msg("新增成功2");
                            layer.closeAll("iframe");
                            //刷新父页面
                            parent.location.reload();
                        }, 500);
                    } else {
                        layer.msg("请求失败", {icon: 7, anim: 6});
                    }
                }
            });
            // layer.close(index0);
        // });

        return false;
    });
});